package dice.sinanya.tools.checkdata.imal;

import dice.sinanya.db.tools.JedisUtil;
import dice.sinanya.entity.EntityTypeMessages;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import redis.clients.jedis.Jedis;

import java.util.Set;

import static dice.sinanya.db.redis.GroupNode.checkGroupNode;
import static dice.sinanya.system.MessagesSystem.loginInfo;

public class GetZk {
    private static final Logger logger = LogManager.getLogger(GetZk.class.getName());

    public static void checkGroupActiveDelete(String groupId) {
        String qqId = String.valueOf(loginInfo.getLoginId());
        try (Jedis jedis = JedisUtil.getJedis(5)) {
            if (qqId.equals(jedis.hget(groupId, "botId"))) {
                jedis.del(groupId);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    public static void clearGroupActive() {
        String qqId = String.valueOf(loginInfo.getLoginId());
        try (Jedis jedis = JedisUtil.getJedis(5)) {
            Set<String> groupList = jedis.keys("*");
            for (String group : groupList) {
                if (qqId.equals(jedis.hget(group, "botId"))) {
                    jedis.del(group);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    public static boolean getInGroupZNodeActive(String groupId, EntityTypeMessages entityTypeMessages) {
        return checkGroupNode(groupId, entityTypeMessages);
    }
}
